/**
 * 配置
 */
import api from '../../tools/api'
import {SUCCESS_STATUS} from '../../config/status'
import promise from '../../tools/promise'
import {
  SETTING_LIST,
  SETTING_CLASS_TREE,
  SETTING_INFO,
  SETTING_SAVE_DATA,
  SETTING_CLASS_INFO
} from '../mutation-types'

const state = {
  pageData: {
    total: 1,
    per_page: 15,
    current_page: 1,
    last_page: 1
  },
  settingList: [],
  classTree: [],
  settingInfo: {},
  classInfo: {},
  saveData: {}
}

const mutations = {
  [SETTING_LIST] (state, payload) {
    state.settingList = payload.data
    state.pageData.total = Number(payload.total)
    state.pageData.per_page = Number(payload.per_page)
    state.pageData.current_page = Number(payload.current_page)
    state.pageData.last_page = Number(payload.last_page)
  },
  [SETTING_CLASS_TREE] (state, payload) {
    state.classTree = payload
  },
  [SETTING_INFO] (state, payload) {
    state.settingInfo = payload
    state.settingInfo.other_info = JSON.stringify(state.settingInfo.other_info)
  },
  [SETTING_SAVE_DATA] (state, data) {
    state.saveData = data
  },
  [SETTING_CLASS_INFO] (state, payload) {
    state.classInfo = payload
  }
}

const actions = {
  // 获取配置列表
  async settingList ({commit}, payload) {
    let listRes = await api('serversetting/setting/Setting/getAppSet', payload)
    if (SUCCESS_STATUS === listRes.status) {
      commit(SETTING_LIST, listRes.res)
    }
  },
  // 添加配置
  async settingAdd ({commit}, payload) {
    let addRes = await api('serversetting/setting/Setting/addSet', payload)
    return promise(addRes)
  },
  // 获取配置分类树
  async classTree ({commit}, payload) {
    let treeRes = await api('serversetting/setting/SettingCategory/getTree', payload)
    if (SUCCESS_STATUS === treeRes.status) {
      commit(SETTING_CLASS_TREE, treeRes.res)
    }
  },
  // 获取某配置详情
  async settingInfo ({commit}, payload) {
    let infoRes = await api('serversetting/setting/Setting/getOneSet', payload)
    if (SUCCESS_STATUS === infoRes.status) {
      commit(SETTING_INFO, infoRes.res)
    }
  },
  // 修改某配置
  async settingEdit ({commit}, payload) {
    let EditRes = await api('serversetting/setting/Setting/editSet', payload)
    return promise(EditRes)
  },
  // 修改分类状态
  async classStatus ({commit}, payload) {
    let statusRes = await api('serversetting/setting/SettingCategory/changeStatus', payload)
    return promise(statusRes)
  },
  // 删除分类
  async classDel ({commit}, payload) {
    let delRes = await api('serversetting/setting/SettingCategory/delOneCate', payload)
    return promise(delRes)
  },
  // 添加分类
  async classAdd ({commit}, payload) {
    let addRes = await api('serversetting/setting/SettingCategory/addCategory', payload)
    return promise(addRes)
  },
  // 获取分类详情
  async classInfo ({commit}, payload) {
    let infoRes = await api('serversetting/setting/SettingCategory/oneCateInfo', payload)
    if (SUCCESS_STATUS === infoRes.status) {
      commit(SETTING_CLASS_INFO, infoRes.res)
    }
  },
  // 修改分类详情
  async classEdit ({commit}, payload) {
    let editRes = await api('serversetting/setting/SettingCategory/editCategory', payload)
    return promise(editRes)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
